<?php

namespace App\Http\Controllers\Admin\system;

use App\Http\Controllers\Admin\CommonController;
use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use App\User,App\Role;
use App\Permission;
use Entrust;
use Route;

class RoleController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
        view()->share([
            '_system' => 'active',
            '_role' => "active",
            'permissions' => Permission::get_permissions()
        ]);
    }

    //显示角色管理界面
    public function index(Request $request){

        $where = function ($query) use ($request) {
            if ($request->has('rolename')) {
                $search = "%" . $request->rolename . "%";
                $query->where('name', 'like',$search);
            }
        };

        $list=Role::with('permissions')->where($where)->orderBy('id')->paginate(config('hwtc.page_size'));
       /* $list=Role::with(['permissions'=>function($query){
            $query->orderBy('model', 'desc');
        }])->where($where)->orderBy('id')->paginate(config('hwtc.page_size'));
return $list;*/
        return view('admin.system.role.index',['list'=>$list]);
    }

    //新增
    public function create()
    {
//        return Permission::get_permissions();
        return view('admin.system.role.create');
    }

    //存储
    public function store(Request $request){
//        return $request->all();
       
        //验证提交数据是否规范
        $this->validate($request, [
            'name' => 'required|max:255|unique:roles',
            'display_name' => 'required|max:255|unique:roles',
        ]);
        $data=$request->all();

        $role=Role::create([
            'name'=> $data['name'],
            'display_name'=> $data['display_name'],
            'description' => $data['description']]);
        if($request->permission_id !=null){
            $role->permissions()->sync($request->permission_id);
        }

        //给用户添加角色
        /*$user=User::where("email",$data['email'])->first();
        foreach($data['role_id'] as $id){
            $user->roles()->attach($id);
        }*/

        //或者
//        $role=Role::find($data['role_id']);
//        $user->attachRole($role);
        return redirect(route('system.role.index'))->with('success', '新增角色成功~');

    }

    public function edit($id){
        $role=Role::with('permissions')->find($id);
//        return $role;
        return view('admin.system.role.edit')->with('role',$role);
    }

    //更新
    public function update(Request $request,$id){
//return $request->all();
        //验证提交数据是否规范
        $this->validate($request, [
            'name' => 'required|max:255',
            'display_name' => 'required|max:255',
        ]);
        $data=$request->all();
        $role=Role::find($id);
        $role->update([
            'name'=> $data['name'],
            'display_name'=> $data['display_name'],
            'description' => $data['description']
        ]);

        //更新角色权限！
        $role->permissions()->detach();  //先删除该角色所有权限
        if($request->permission_id !=null) {
            $role->permissions()->sync($request->permission_id);
        }
        return redirect(route('system.role.index'))->with('success', '编辑角色成功~');
    }

    public function destroy($id){
        $role=Role::find($id);
        DB::beginTransaction();
        try{
            $role->permissions()->detach(); //删除该用户所有角色
            Role::destroy($id); //删除该用户
        }catch(\Exception $e){
            DB::rollback();
            return $e->getMessage();
        }

        return redirect(route('system.role.index'))->with('success', '删除角色成功~');
    }

}